package com.ktjiaoyu.ktmall.servlet.api;

import com.alibaba.fastjson.JSON;
import com.ktjiaoyu.ktmall.entity.Category;
import com.ktjiaoyu.ktmall.service.CategoryService;
import com.ktjiaoyu.ktmall.service.impl.CategoryServiceImpl;
import com.ktjiaoyu.ktmall.utils.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.Buffer;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "CategoryServlet", urlPatterns = "/api/category/*")
public class CategoryServlet extends HttpServlet {
    private final CategoryService categoryService = new CategoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        req.setCharacterEncoding("utf-8");

        String path = req.getPathInfo() ;
        R result;
        if("/tree".equals(path)){
            try {
                List<Category> tree = categoryService.getTree();
                result = R.ok(tree);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else if("/saveOrUpdate".equals(path)){ // 保存或更新
            try {
                // 获取参数
                BufferedReader reader = req.getReader();
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                Category category = JSON.parseObject(sb.toString(), Category.class);
                // 保存或更新
                boolean saveOrUpdate = categoryService.saveOrUpdate(category);
                // 返回结果
                result = saveOrUpdate ? R.ok() : R.fail();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else if ("/del".equals(path)) {
            try {
                Long id = Long.valueOf(req.getParameter("id"));
                boolean deleted = categoryService.removeById(id);
                result = deleted ? R.ok(id) : R.fail(id);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else {
            try {
                List< Category> list = categoryService.findAll();
                result = R.ok();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        PrintWriter out = resp.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        this.doGet(req, resp);
    }
}
